
$(document).ready(function(){
	//加载验证码
	loadImg();
	//开启jquery校验
	validateLoginForm();
})

function loadImg() {
	$("#img-vcode").attr("src","verifycode.html?t="+(new Date().getTime()));
}

/**
 * 登录事件
 * @returns
 */
$("#loginAction").click(function(){
	if ($("#loginForm").valid()) {
		var $getFormData = $("#loginForm").serialize();
		$.post("dologin", $getFormData, callBackForLogin, "json");
	}
})

function callBackForLogin(data) {
	$("#loginForm")[0].reset();
	if (data.msg == "success") {
		//$(window).attr("location", './index');
		window.location.href = "./index.html"
	} else {	
		if ($("#err-tip").attr("class") == "alert-info") {
			$("#err-tip").removeClass("alert-info").addClass("alert-danger");
		}
		$("#err-tip").show();
		$("#err-tip").html(data.msg);
	}
}


/**
 * 登录表单的jquery验证
 * @returns
 */
function validateLoginForm(){

	$("#loginForm").validate({
		onsubmit:true,// 是否在提交是验证
	　　	onfocusout:false,// 是否在失去焦点时验证
	　　	onkeyup :false,// 是否在敲击键盘时验证
		rules:{
			loginCode: {
				required: true,
			},
			password: {
				required: true,
			},
			vcode:{
				required: true,
			}	
		},
		messages: {
			loginCode: {
				required: "请填写用户名",
			},
			password: {
				required: "请填写密码",
			},
			vcode:{
				required: "验证码不能为空",
			}
			
			
		},
		
		errorPlacement: function(error, element){
			$("#err-tip").show();
			$("#err-tip").removeClass("alert-info").addClass("alert-danger");
			$("#err-tip").html(error);
		},
		submitHandler: function(form){
			
		},
		invalidHandler: function(form, validator) {
			return false;}
		
	})
}
